// components/my-bottom/my-bottom.js
var app = getApp()
var myhost = app.globalData.myhost

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    book: {
      type: Object,
      default: [],
      observer: function (newVal) {
        // console.log(newVal)
        this.setData({
          mbook: newVal
        })
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    isload: false,
    books: [],
    mbook: {}
  },
  lifetimes: {
    attached: function () {
      // console.log('botton',this.data.mbook)
      var booklist = []
      if (app.globalData.userId) {
        wx.request({
          url: myhost + '/collect/collectOfUserId?userId=' + app.globalData.userId,
          success: (res) => {
            console.log(res.data)
            for (var i = 0; i < res.data.length; i++) {
              booklist.push(res.data[i].bookId)
            }
            this.setData({
              books: booklist
            })
          }
        })
        setTimeout(() => {
          console.log('botton', this.data.books.indexOf(this.data.mbook.id))
          if (this.data.books.indexOf(this.data.mbook.id) == -1) {
            this.setData({
              isload: false
            })
          } else {
            this.setData({
              isload: true
            })
          }
        }, 500)
      }
    },
  },
  /**
   * 组件的方法列表
   */
  methods: {
    shujia: function () {
      if(app.globalData.userId){
        if(this.data.isload){
          console.log("移出书架")
          wx.request({
            url: myhost+'/collect/delete?userId='+app.globalData.userId+'&bookId='+this.data.mbook.id,
            success:(res)=>{
              console.log(res)
              wx.showToast({
                title: '已移出书架',
                icon: 'success',
                duration: 2000
              })
              this.setData({
                isload:false
              })
            }
          })
        }else{
          console.log("加入书架")
          console.log(myhost+'/collect/add?userId='+app.globalData.userId+'&bookId='+this.data.mbook.id)
          wx.request({
            url: myhost+'/collect/add?userId='+app.globalData.userId+'&bookId='+this.data.mbook.id,
            method:'POST',
            success:(res)=>{
              console.log(res)
              wx.showToast({
                title: '已加入书架',
                icon: 'success',
                duration: 2000
              })
              this.setData({
                isload:true
              })
            }
          })
        }
      }else{
        wx.showToast({
          title: '请先登录',
          icon: 'error',
          duration: 2000
        })
      }
    },
    toubi: function () {
      if (app.globalData.userId) {
        wx.showToast({
          title: '投币成功',
          icon: 'success',
          duration: 2000
        })
        var coinnum = 0
        console.log(myhost + '/consumer/selectByPrimaryKey?id=' + app.globalData.userId)
        wx.request({
          url: myhost + '/consumer/selectByPrimaryKey?id=' + app.globalData.userId,
          success: (res) => {
            // console.log(res.data.coinNum)
            coinnum = res.data.coinNum
          }
        })
        setTimeout(() => {
          wx.request({
            url: myhost + '/liu/modifyCoinNum?id=' + app.globalData.userId + '&coin_num=' + (coinnum - 1),
          })
        }, 500)
      } else {
        wx.showToast({
          title: '请先登录',
          icon: 'error',
          duration: 2000
        })
      }
    },
    toReader: function (e) {
      // console.log('botton',e);
      const bookid = e.currentTarget.dataset.bookid;
      const name = e.currentTarget.dataset.name;
      var that = this;
      let serialNumber = '1';
      if (e.currentTarget.dataset.serialnumber) {
        serialNumber = e.currentTarget.dataset.serialnumber;
      }
      wx.navigateTo({
        url: '../reader/reader?id=' + bookid + '&serialNumber=' + serialNumber + '&title=' + name,
      });
    },
  }
})